package lapsnake.workspace;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JPanel;
import javax.swing.JToggleButton;

import owg.util.Calc;
import owg.util.awt.VerticalLayout;
import lapsnake.main.LapSnake;
import lapsnake.model.LapModule;
import lapsnake.model.PyAST;
import lapsnake.model.data.TypedListener;
import lapsnake.model.stmt.LapClassDef;

public class Workspace extends JPanel 
{
	private static final long serialVersionUID = 2384358818579008975L;
	
	private JPanel projectPanel;
	private JDesktopPane desktop;
	
	public Workspace() 
	{
		try 
		{
			PyAST ast = new PyAST("wat", Calc.readFileString(new FileInputStream(new File("sample/sample.py"))));
			
			ast.uncompileAST();
			System.out.println(ast.visualizeScriptNodes());
			
			LapModule module = ast.getRoot();
			LapClassDef[] classes = module.getClasses();
			if(classes.length > 0)
			{
				LapClassDef class0 = classes[0];
				
				setLayout(new GridBagLayout());
				GridBagConstraints c = new GridBagConstraints();
				c.gridx = 0;
				c.gridy = 0;
				c.fill = GridBagConstraints.BOTH;
				c.weightx = 0;
				c.weighty = 1;
				
				projectPanel = new JPanel();
				projectPanel.setLayout(new VerticalLayout(0, VerticalLayout.BOTH, VerticalLayout.TOP));
				JToggleButton modeButton = new JToggleButton("Visual Mode");
				TypedListener<Boolean> modeButtonChanger = (property, oldValue)->{ modeButton.setSelected(property.get()); };
				LapSnake.settings.visualMode.addListener(modeButtonChanger);
				modeButton.addActionListener(ev->{LapSnake.settings.visualMode.set(modeButton.isSelected(), modeButtonChanger, true);});
				modeButton.setSelected(LapSnake.settings.visualMode.get());
				projectPanel.add(modeButton);
				JButton runButton = new JButton("Run");
				runButton.setEnabled(false);
				//runButton.addActionListener(ev->{ast.run();});
				projectPanel.add(runButton);
				add(projectPanel, c);
				
				c.gridx ++;
				c.weightx = 1;
				
				desktop = new JDesktopPane();
				desktop.setBackground(Color.LIGHT_GRAY);
				add(desktop, c);
				
				desktop.add(new ClassFrame(this, class0));
			}
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
	}
	
}
